// pages/me/storeform/storeform.js
const { toUrl } = getApp();
import { request } from "@/utils/request";
Page({
    toUrl,
    /**
     * 页面的初始数据
     */
    data: {
        privierUrl: "",
        image: [],
        prviewImg:[],
        date: '',
        content: '',
        bill_price: '',
        today: '',
        lastday: '',
        priceval: '',
        report: {
            suppliers_name:"",
            realname:"",
            bill_time:"",
            statement_tips:""
        }
    },
    changePhoto(e) {
        let { img, url } = e.detail;
        let list = this.data.prviewImg 
       
        list.push(img)
        this.setData({
          privierUrl: img,
          image: list,
        });
    },
    preview(e){
    
        let {index} = e.currentTarget.dataset
        let Imglist = this.data.image
        wx.previewImage({
          current: Imglist[index], // 当前显示图片的http链接
          urls: Imglist // 需要预览的图片http链接列表
        })
    },
    close(e) {
        console.log(e);
        const index = e.currentTarget.dataset.index
        const imgList = this.data.image
        imgList.splice(index,1)
        this.setData({
          image: imgList
        })
    },
    bindDateChange: function(e) {
        // const time = e.detail.value.split('-')
        // const val = time[0] + '年' + time[1] + '月' + time[2] + '日'
        this.setData({
          date: e.detail.value
        })
    },
    getTip() {
        const that = this
        wx.getStorage({
            key: '_userInfo',
            success(storageRes) {
                const token = storageRes.data.token
                request('user/report',{token}).then(res => {
                    that.setData({
                        report: res.data,
                        date: res.data.bill_time,
                        // lastday: res.data.bill_time,
                        content: res.data.statement_tips
                    })
                })
            }
        })
    },
    priceinput(e) {
        this.setData({
            bill_price: e.detail.value
        })
    },
    receipt() {
        const that = this
        wx.getStorage({
            key: '_userInfo',
            success(tokenRes) {
                const token = tokenRes.data.toke
                const data = {
                    token,
                    bill_time: that.data.date,
                    bill_price: that.data.bill_price,
                    images: that.data.image.join(',')
                }
                request('user/reportsubmit',{data}).then(res => {
                    wx.showToast({
                        title: "提交成功"
                    });
                    that.setData({
                        date: that.data.lastday,
                        bill_price: '',
                        image: []
                    })
                })
            }
        })
    },
    generateDate() {
        const date = new Date()
        const year = date.getFullYear()
        const month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1)
        const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
        if(day != 1) {
            this.setData({
                date: year + '-' + month + '-' + (Number(day) - 1)
            })
        }else {
            // const lastday = date.getDate(0) > 9 ? date.getDate(0) : '0' + date.getDate(0)
            const currentDate = new Date();
            currentDate.setMonth(currentDate.getMonth()); // 设置为上个月
            currentDate.setDate(0); // 设置为上个月的最后一天

            const lastday = currentDate.getDate();
            this.setData({
                date: year + '-' + currentDate.getMonth()+1 + '-' + lastday,
                lastday: year + '-' + currentDate.getMonth()+1 + '-' + lastday
            })
        }
        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getTip()
        this.generateDate()
        const date = new Date()
        const year = date.getFullYear()
        const month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1)
        const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
        this.setData({
            today: year + '-' + month + '-' + day
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})